﻿using System.Collections.Generic;
using InseSoft.Infraestructure;
using InseSoft.Models.Entities;
using InseSoft.Views;

namespace InseSoft.ViewModels
{
    public class WindowLauncher : IWindowLauncher
    {
        private readonly Dictionary<AppView, ViewViewModel> config = new Dictionary<AppView, ViewViewModel>();

        public WindowLauncher()
        {
            config.Add(AppView.Empresas, new ViewViewModel(() => new EmpresaWindow(), () => new CatalogEntityViewModel<Empresa>()));
            config.Add(AppView.Clientes, new ViewViewModel(() => new ClienteWindow(), () => new CatalogEntityViewModel<Cliente>()));
            config.Add(AppView.Lineas, new ViewViewModel(() => new LineaWindow(), () => new CatalogEntityViewModel<Linea>()));
            config.Add(AppView.Materiales, new ViewViewModel(() => new MaterialWindow(), () => new CatalogEntityViewModel<Material>()));
            config.Add(AppView.TipoCambio, new ViewViewModel(() => new TipoCambioWindow(), () => new CatalogEntityViewModel<TipoCambio>()));
            config.Add(AppView.Cotizacion, new ViewViewModel(() => new CotizacionWindow(), () => new CotizacionViewModel()));
        }

        public Dictionary<AppView, ViewViewModel> Configuration
        {
            get { return config; }
        }

        public void Show(AppView view)
        {
            var viewViewModel = config[view];
            DisplayWindow(viewViewModel.View.Invoke(), viewViewModel.ViewModel.Invoke());
        }

        private static void DisplayWindow(IWindow view, IEntityViewModel viewModel)
        {
            DIFactory.Kernel.Inject(viewModel);
            viewModel.Initialize();
            view.DataContext = viewModel;
            view.ShowDialog();
            viewModel.Dispose();
        }
    }
}